package com.wap.framework.admin.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;


import com.wap.framework.model.PVCounter;
import com.wap.framework.service.PVCounterService;

public class PVCounterManagerAction extends DispatchAction {
	private PVCounterService pvcounterService;

	public void setPvcounterService(PVCounterService pvcService) {
		this.pvcounterService = pvcService;
	}

	/**
	 * list all PVCounter
	 */
	public ActionForward listByDate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String dateStr = request.getParameter("date");
		String mode = request.getParameter("mode");
		if (dateStr == null || "".equals(dateStr)) {
			dateStr = getYesToday();
		}
		if (mode == null || "".equals(mode)) {
			mode = "pv";
		}
		List<PVCounter> pvcounters = null;
		if ("ad".equals(mode)) {
			pvcounters = pvcounterService.listADClikeByDate(dateStr);
			request.setAttribute("dateStr", dateStr);
			request.setAttribute("mode", mode);
			request.setAttribute("pvcounters", pvcounters);
			return mapping.findForward("fwd_view_adclick");
		} else {
			pvcounters = pvcounterService.listPVByDate(dateStr);
			request.setAttribute("dateStr", dateStr);
			request.setAttribute("mode", mode);
			request.setAttribute("pvcounters", pvcounters);
			return mapping.findForward("fwd_view_pv");
		}

	}

	public String getYesToday() {
		java.util.Date date = new java.util.Date();
		date.setDate(date.getDate() - 1);
		java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
				"yyyy-MM-dd");
		String day = formatter.format(date);
		return day;
	}

	/**
	 * get PVCounter
	 */
	@SuppressWarnings("unchecked")
	public ActionForward getPVCounterById(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String pvcId = request.getParameter("pvcId");
		PVCounter pvc = this.pvcounterService.getPVCounterById(Integer
				.valueOf(pvcId));
		request.setAttribute("pvc", pvc);
		return mapping.findForward("fwd_info_pvc");
	}

}
